﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PMS.Logic.CommonAjax
{
    public partial class UserRelateAjax : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String methodName = Request["method"];
            Type type = this.GetType();
            MethodInfo method = type.GetMethod(methodName);
            if (method == null) throw new Exception("method is null");
            try
            {

                method.Invoke(this, null);
            }
            catch
            {
                throw;
            }

        }


        //获取父权限
        public void GetParentPerm()
        {

            Response.Write(JsonHelper.toJson(DALSession.Current.BD_PermissionDAL.GetListBy(p => p.IsFatherNode == "0").Select(p => new { value = p.Id, text = p.Name })));

        }


        //获取非父权限
        public void GetSubPerm()
        {

            Response.Write(JsonHelper.toJson(DALSession.Current.BD_PermissionDAL.GetListBy(p => p.IsFatherNode == "1").Select(p => new { value = p.Id, text = p.Name })));
        }



        //根据模块名称获取非父权限
        public void GetSubPermByModelName()
        {
            string modelName = Request.QueryString["modelName"];

            var father1 = DALSession.Current.View_BD_PermissionDAL.GetListBy(p => p.Parent != 0 && p.ParentName.Contains(modelName) && p.IsFatherNode == "1").Select(p => new { value = p.Id, text = p.Name }).ToList();

            var father2 = DALSession.Current.View_BD_PermissionDAL.GetListBy(p => p.Parent != 0 && p.ParentName.Contains(modelName) && p.IsFatherNode == "0").Select(p => new { value = p.Id, text = p.Name }).ToList();
            foreach (var item in father2)
            {
                var sub1 = DALSession.Current.View_BD_PermissionDAL.GetListBy(p => p.Parent == item.value && p.IsFatherNode == "1").Select(p => new { value = p.Id, text = p.Name }).ToList();
                var sub2 = DALSession.Current.View_BD_PermissionDAL.GetListBy(p => p.Parent == item.value && p.IsFatherNode == "0").Select(p => new { value = p.Id, text = p.Name }).ToList();
                father1.AddRange(sub1);
                foreach (var child in sub2)
                {
                    var son = DALSession.Current.View_BD_PermissionDAL.GetListBy(p => p.Parent == child.value).Select(p => new { value = p.Id, text = p.Name }).ToList();
                    father1.AddRange(son);
                }

            }
            Response.Write(JsonHelper.toJson(father1));

        }

        //获取部门
        public void GetDep()
        {

            Response.Write(JsonHelper.toJson(DALSession.Current.BD_DepInfoDAL.GetListBy(p => p.DepState == "0").Select(p => new { value = p.ID, text = p.DepName })));

        }

        //获取用户名
        public void GetAllUserName()
        {

            Response.Write(JsonHelper.toJson(DALSession.Current.BD_UserInfoDAL.GetListBy(p => p.UserState == "0").Select(p => new { value = p.ID, text = p.UserName })));

        }

        public void GetAllDepName()
        {

            Response.Write(JsonHelper.toJson(DALSession.Current.BD_DepInfoDAL.GetListBy(p => p.DepState == "0").Select(p => new { value = p.DepName, text = p.DepName })));

        }
        //根据用户ID获取用户部门id
        public void GetDepbyUserId()
        {
            int id = OperateContext.SessBasicInfo.UserInfo.Id;
            int depid = (int)DALSession.Current.BD_UserInfoDAL.GetListBy(p => p.ID == id).FirstOrDefault().DepId;
            Response.Write(JsonHelper.toJson(new { DepID = depid }));
        }
        //获取当前登录人姓名
        public void GetLoginPerson()
        {
            string name = OperateContext.SessBasicInfo.UserInfo.Name.ToString();
            Response.Write(JsonHelper.toJson(new { username = name }));
        }
        //获取当前登录人ID
        public void GetLoginPersonID()
        {
            string userid = OperateContext.SessBasicInfo.UserInfo.Id.ToString();
            Response.Write(JsonHelper.toJson(new { id = userid }));
        }
        //获取当前登录人ID与部门ID
        public void GetLoginPersonIDDepID()
        {
            string userid = OperateContext.SessBasicInfo.UserInfo.Id.ToString();
            string depID = OperateContext.SessBasicInfo.UserInfo.DepId.ToString();
            Response.Write(JsonHelper.toJson(new { id = userid, depID = depID }));
        }
        //获取一级部门信息
        public void GetDepInfo()
        {
            Response.Write(JsonHelper.toJson(DALSession.Current.BD_DepInfoDAL.GetListBy(p => p.DepState == "0").Select(p => new { value = p.ID, text = p.DepName })));
        }
        public void GetDepName()
        {
            Response.Write(JsonHelper.toJson(DALSession.Current.BD_DepInfoDAL.GetListBy(p => p.DepState == "0").Select(p => new { value = p.DepName, text = p.DepName })));
        }
        //获取部门下的人员
        public void GetPersonByDepId()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["depID"]))
            {
                int depid = Commons.String2Int32(Request.QueryString["depID"]);
                Response.Write(JsonHelper.toJson(DALSession.Current.BD_UserInfoDAL.GetListBy(p => p.DepId == depid&&p.UserState=="0").Select(p => new { value = p.ID, text = p.UserName })));
            }
            else
            {
                Response.Write(JsonHelper.toJson(DALSession.Current.BD_UserInfoDAL.GetListBy(p=>p.UserState=="0").Select(p => new { value = p.ID, text = p.UserName })));
            }
        }
        //根据一级部门ID获取一级部门名称
        public void GetDepNameBydepID()
        {
            if (Request.QueryString["depID"] != null)
            {
                int depid = Commons.String2Int32(Request.QueryString["depID"]);
                if (depid != 0)
                {
                    var DepName = DALSession.Current.BD_DepInfoDAL.GetListBy(p => p.DepState == "0" && p.ID == depid).FirstOrDefault().DepName;
                    Response.Write(JsonHelper.toJson(new { depname = DepName }));
                }
            }
        }

        /// <summary>
        /// 根据ComboId获取下来菜单人员
        /// </summary>
        public void GetComboPeo()
        {
            if (Request.QueryString["comboId"] != null)
            {

                int comboId = Commons.String2Int32(Request.QueryString["comboId"]);
                List<int> userIds = DALSession.Current.BD_ComboUserDAL.GetListBy(c => c.ComboId == comboId).Select(c => c.UserId).ToList();
                Response.Write(JsonHelper.toJson(DALSession.Current.BD_UserInfoDAL.GetListBy(u => userIds.Contains(u.ID)).Select(c => new { value = c.ID, text = c.UserName })));
            }
        }
        /// <summary>
        /// 获取ComboId列表
        /// </summary>
        public void GetComboId()
        {
            Response.Write(JsonHelper.toJson(DALSession.Current.BD_ComboPickUserDAL.GetListBy(c => 1 == 1).Select(c => new { value = c.ID, text = c.ComboName })));
        }

    }
}